package cn.edu.zjgsu.ito.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.zjgsu.ito.exception.DateWrongException;
import cn.edu.zjgsu.ito.service.UserManageServiceI;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.RefundReasonShowVo;
import cn.edu.zjgsu.ito.vo.UserAddressVo;
import cn.edu.zjgsu.ito.vo.UserDemandOrderShowVo;
import cn.edu.zjgsu.ito.vo.UserHeadUrlShowVo;
import cn.edu.zjgsu.ito.vo.UserInfomationSaveVo;
import cn.edu.zjgsu.ito.vo.UserInfomationShowVo;
import cn.edu.zjgsu.ito.vo.UserPurchaseOrderVo;

/**
 * 用户管理控制类
 * */
@Controller
@RequestMapping("userManageController")
public class UserManageController {

	@Autowired
	private UserManageServiceI userManageService;

	/**
	 * 用户查看我的求购信息接口<br/>
	 * 通过用户id 查看用户的求购信息
	 * 
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "userDemandOrder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userDemandOrder(HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserDemandOrderShowVo> userDemandOrderShowVo = userManageService
				.userDemandOrder(userId);

		result.put("msg", userDemandOrderShowVo);
		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 用户筛选我的求购信息接口<br/>
	 * 通过用户id 查看用户的求购信息
	 * 0-未接单  1-进行中  2-已完成  3-全部
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "userChooseDemandOrder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userChooseDemandOrder(int temp,HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserDemandOrderShowVo> userDemandOrderShowVo = userManageService
				.userChooseDemandOrder(temp,userId);

		result.put("msg", userDemandOrderShowVo);
		result.put("code", Constant.OK);

		return result;
	}
	
	/**
	 * 通过商品名搜索我的请求订单
	 * @param temp
	 * @param session
	 * @return
	 */
	@RequestMapping(value="userDemandOrderSelectBy",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userDemandOrderSelectBy(String temp,HttpSession session){
		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserDemandOrderShowVo> userDemandOrderShowVo = userManageService
				.userDemandOrderSelectBy(temp,userId);

		result.put("msg", userDemandOrderShowVo);
		result.put("code", Constant.OK);

		return result;
	}
	
	
	/**
	 * 用户查看我的代购信息接口<br/>
	 * 
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "userPurchaseOrder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userPurchaseOrder(HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserPurchaseOrderVo> userPurchaseOrderVos = userManageService
				.userPurchaseOrder(userId);

		result.put("msg", userPurchaseOrderVos);
		result.put("code", Constant.OK);

		return result;
	}
	
	/**
	 * 用户筛选我的代购信息接口<br/>
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "userChoosePurchaseOrder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userChoosePurchaseOrder(int temp,HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserPurchaseOrderVo> userPurchaseOrderVos = userManageService
				.userChoosePurchaseOrder(temp,userId);

		result.put("msg", userPurchaseOrderVos);
		result.put("code", Constant.OK);

		return result;
	}
	
	
	/**
	 * 用户搜索我的代购信息接口<br/>
	 * 
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "userPurchaseOrderSelectBy", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userPurchaseOrderSelectBy(String temp,HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());

		Map<String, Object> result = new HashMap<String, Object>();

		List<UserPurchaseOrderVo> userPurchaseOrderVos = userManageService
				.userPurchaseOrderSelectBy(temp,userId);

		result.put("msg", userPurchaseOrderVos);
		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 用户查看个人信息接口<br/>
	 * 根据当前用户的id，查看用户的个人信息
	 * 
	 * @param userIdVo
	 *            用户id
	 * @return
	 * */
	@RequestMapping(value = "infomation", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> infomation(HttpSession session) {
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		System.out.println("用户的id:" + userId);
		Map<String, Object> result = new HashMap<String, Object>();

		UserInfomationShowVo userInfomationShowVo = userManageService
				.getInfomation(userId);

		result.put("msg", userInfomationShowVo);
		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 用户编辑个人信息接口<br/>
	 * 根据当前用户的id，修改用户的个人信息
	 * 
	 * @param userInfomationSaveVo
	 *            用户个人信息
	 * @return
	 * */
	@RequestMapping(value = "updateInfomation", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateInfomation(
			UserInfomationSaveVo userInfomationSaveVo, HttpSession session) {
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			userManageService.editUserInfomation(userInfomationSaveVo, userId);
			result.put("code", Constant.OK);
		} catch (DateWrongException e) {
			result.put("code", Constant.WRONG_BIRTH_CODE);
		}
		return result;
	}

	/**
	 * 用户地址管理接口<br/>
	 * 根据用户id，返回用户地址信息
	 * 
	 * @param userIdVo
	 * @return
	 */
	@RequestMapping(value = "address", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> address(HttpSession session) {
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		Map<String, Object> result = new HashMap<String, Object>();

		List<UserAddressVo> userAddressVo = userManageService
				.getAddress(userId);

		result.put("msg", userAddressVo);
		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 用户添加地址接口<br/>
	 * 根据用户传过来的信息，添加地址
	 * 
	 * @param userAddressVo
	 * @return
	 */
	@RequestMapping(value = "addAddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAddress(UserAddressVo userAddressVo,
			HttpSession session) {

		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		userAddressVo.setUserId(userId);

		Map<String, Object> result = new HashMap<String, Object>();

		userManageService.addAddress(userAddressVo);

		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 删除用户地址接口<br/>
	 * 根据用户传过来的地址id,删除对应地址信息
	 * 
	 * @param userDeleteAddressVo
	 * @return
	 */
	@RequestMapping(value = "deleteAddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAddress(UserAddressVo userAddressVo) {

		Map<String, Object> result = new HashMap<String, Object>();

		userManageService.deleteAddress(userAddressVo);

		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 获取地址详细信息
	 * 
	 * @param addressId
	 * @return
	 */
	@RequestMapping(value = "addressMessage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addressMessage(int addressId) {

		Map<String, Object> result = new HashMap<String, Object>();

		UserAddressVo userAddressVo = userManageService
				.getAddressMessage(addressId);

		result.put("msg", userAddressVo);
		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 修改地址信息接口<br/>
	 * 根据地址id，修改地址信息
	 * 
	 * @param userDeleteAddressVo
	 * @return
	 */
	@RequestMapping(value = "updateAddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateAddress(UserAddressVo userAddressVo) {

		Map<String, Object> result = new HashMap<String, Object>();

		userManageService.updateAddress(userAddressVo);

		result.put("code", Constant.OK);

		return result;
	}

	/**
	 * 用户查看具体发布信息接口<br/>
	 * 通过求购单id，查看具体信息
	 * 
	 * @param demandIdVo
	 * @return
	 */
	/*
	 * @RequestMapping(value="userDemandOrderMessage",method=RequestMethod.POST)
	 * 
	 * @ResponseBody public Map<String,Object> userDemandOrderMessage(DemandIdVo
	 * demandIdVo){
	 * 
	 * Map<String,Object> result = new HashMap<String, Object>();
	 * 
	 * DemandOrderMessageVo demandOrderMessageVo =
	 * userManageService.userDemandOrderMessage(demandIdVo);
	 * 
	 * result.put("msg", demandOrderMessageVo); result.put("code", Constant.OK);
	 * 
	 * return result; }
	 *//**
	 * 我的代购订单详细信息接口<br/>
	 * 
	 * @param orderIdVo
	 * @return
	 */
	/*
	 * @RequestMapping(value="userPurchaseOrderMessage",method=RequestMethod.POST
	 * )
	 * 
	 * @ResponseBody public Map<String,Object>
	 * userPurchaseOrderMessage(OrderIdVo orderIdVo){
	 * 
	 * Map<String,Object> result = new HashMap<String, Object>();
	 * 
	 * DemandOrderMessageVo demandOrderMessageVo =
	 * userManageService.userPurchaseOrderMessage(orderIdVo);
	 * 
	 * result.put("msg", demandOrderMessageVo); result.put("code", Constant.OK);
	 * 
	 * return result; }
	 */

	/**
	 * 头像上传 工具类
	 * 
	 * @param sourcePath
	 * @param request
	 * @return 返回路径
	 */
	@RequestMapping(value = "imageUpload", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> headPortraitUpload(MultipartFile file,HttpServletRequest request, HttpSession session) {
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
//		int userId = 1;
		Map<String, Object> result = new HashMap<String, Object>();
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				// 文件保存路径
				String path = request.getSession().getServletContext().getRealPath("/");
				String headUrl = userManageService.headPortraitUpload(file, path,userId);
				UserHeadUrlShowVo userHeadUrlShowVo = new UserHeadUrlShowVo();
				userHeadUrlShowVo.setHeadUrl(headUrl);
				result.put("msg", userHeadUrlShowVo);
				result.put("code", Constant.OK);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 图片批量上传工具
	 * @param files
	 * @param request
	 * @param session
	 * @param temp0->图片    1->店铺
	 * @return
	 */
	@RequestMapping(value="moreImageUpload",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> imageUpload(MultipartFile[] files,int temp,HttpServletRequest request, HttpSession session){
		Map<String, Object> result = new HashMap<String, Object>();
		List<String> tradeUrlArrayList = new ArrayList<String>();
		String path = request.getSession().getServletContext().getRealPath("/");
		try {
			for(MultipartFile mu : files){
				if (!mu.isEmpty()) {
					String savaPath = userManageService.imageUpload(mu,path,temp);
					tradeUrlArrayList.add(savaPath);
				}
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
		result.put("msg", tradeUrlArrayList);
		result.put("code",Constant.OK);
		return result;
	}
	
	/**
	 * 用户查看退款理由
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="lookRefund",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> lookRefund(OrderIdVo orderIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		RefundReasonShowVo refundReasonShowVo = userManageService.lookRefund(orderIdVo);
		
		result.put("msg", refundReasonShowVo);
		result.put("code", Constant.OK);
		
		return result;
		}
	
	
	/**
	 * 用户修改密码
	 * @param password
	 * @return
	 */
	@RequestMapping(value="changePassword",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> changePassword(String password,HttpSession session){
		Map<String, Object> result = new HashMap<String, Object>();
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		userManageService.changePassword(userId,password);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	
	/**
	 * 用户查看好友个人信息接口<br/>
	 * 根据当前用户的id，查看用户的个人信息
	 * 
	 * @param userIdVo
	 *            用户id
	 * @return
	 * */
	@RequestMapping(value = "friendInfomation", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> friendInfomation(int userId,HttpSession session) {
//		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		System.out.println("用户的id:" + userId);
		Map<String, Object> result = new HashMap<String, Object>();

		UserInfomationShowVo userInfomationShowVo = userManageService
				.getInfomation(userId);

		result.put("msg", userInfomationShowVo);
		result.put("code", Constant.OK);

		return result;
	}
	
}
